using System;
using System.Data;
using System.Windows.Forms;
using System.Resources;
using System.Text.RegularExpressions;
using HSHLogicLayer.AuthWS;
using HSHLogicLayer;

namespace TaskVision
{
	public partial class AddUserForm : System.Windows.Forms.Form
	{
		private ResourceManager m_ResourceManager = new ResourceManager("TaskVision.Localize", System.Reflection.Assembly.GetExecutingAssembly());

		
		public AddUserForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

		}

		private void btnAdd_Click(System.Object sender, System.EventArgs e)
		{
			if (IsFormValid())
			{																													 
				UserInformation newUserInfo = new UserInformation();

				newUserInfo.UserName = txtUserName.Text.Trim();
				newUserInfo.UserPassword = txtPassword.Text;
				newUserInfo.UserFullName = txtUserFullName.Text.Trim();
				newUserInfo.UserEmail = txtUserEmail.Text.Trim();
				newUserInfo.IsAdministrator = cbAdministrator.Checked;
				newUserInfo.IsAccountLocked = cbLocked.Checked;
                newUserInfo.MustChangePassword = cbMustChangePassword.Checked;

				int userID = -1;

                DataLayerResult dlResult = DataLayerSingleton.Instance().InsertUser(ref userID, newUserInfo);

				// If we were able to add the user to the database, update our local data
				if (dlResult.ResultType == DataLayerResultType.Success)
				{
					if (userID > 0)
					{
						DataRow dr = DataLayerSingleton.Instance().DsLookupTables.Users.NewRow();
						dr["UserID"] = userID;
						dr["UserName"] = newUserInfo.UserName;
						dr["UserFullName"] = newUserInfo.UserFullName;
						dr["UserEmail"] = newUserInfo.UserEmail;
						dr["IsAdministrator"] = newUserInfo.IsAdministrator;
						dr["IsAccountLocked"] = newUserInfo.IsAccountLocked;
                        dr["MustChangePassword"] = newUserInfo.MustChangePassword;
                        DataLayerSingleton.Instance().DsLookupTables.Users.Rows.Add(dr);

						this.DialogResult = DialogResult.OK;
						this.Close();
					}
					else
					{
						HebMessageBox.Show(m_ResourceManager.GetString("That_user_name_is_already_taken"));
						txtUserName.Focus();
					}
				}
				else
				{
					HebMessageBox.Show(m_ResourceManager.GetString("Unable_to_add_user_at_this_time"));
					this.DialogResult = DialogResult.None;
				}
			}
		}

		private void btnCancel_Click(System.Object sender, System.EventArgs e)
		{
			this.Close();
		}

		private bool IsFormValid()
		{
			// check the fields for valid data and
			// display message boxes if neccessary

            if (txtUserName.Text.Length < 3)
			{
				HebMessageBox.Show(m_ResourceManager.GetString("User_name_must_be_between"));
				return false;
			}
			else if (txtPassword.Text.IndexOf(" ") > -1)
			{
				HebMessageBox.Show(m_ResourceManager.GetString("Password_may_not_contain_spaces"));
				return false;
			}
			else if (txtPassword.Text.Length < 3)
			{
				HebMessageBox.Show(m_ResourceManager.GetString("Password_must_be_between"));
				return false;
			}
			else if (txtPassword.Text != txtConfirmPassword.Text)
			{
				HebMessageBox.Show(m_ResourceManager.GetString("Password_fields_do_not_match"));
				return false;
			}
			else if (txtUserFullName.Text.Trim().Length < 3)
			{
				HebMessageBox.Show(m_ResourceManager.GetString("Full_name_must_be_between"));
				return false;
			}
			
			Regex r = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
			if (!r.IsMatch(txtUserEmail.Text))
			{
				HebMessageBox.Show(m_ResourceManager.GetString("Email_address_is_not_valid"));
				return false;
			}
			
			return true;
		}

		
	}
}
